package com.guodi.map.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.core.tool.api.R;
import com.guodi.map.entity.MapMenuUser;
import com.guodi.map.service.IMapMenuUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @描述：菜单收藏操作
 * @作者: 卢顺
 * @日期: 2022-12-07
 */
@RestController
@RequestMapping("/MapMenuUser")
@Api(value = "API - MapMenuUserController", tags = "菜单收藏")
public class MapMenuUserController extends BaseController {

	@Autowired
	private IMapMenuUserService menuUserService;

	/**
	 * @描述：收藏列表
	 * @作者: 卢顺
	 * @日期: 2022-12-07
	 */
	@GetMapping("/list")
	@ApiOperation(value = "收藏列表", notes = "收藏列表")
	public R list() {
		List<MapMenuUser> mapMenuUserList =  getInfo();
		StringBuffer stringBuffer = new StringBuffer();
		if(mapMenuUserList != null && mapMenuUserList.size() >0){
			for (int i = 0; i<mapMenuUserList.size();i++){
				if(stringBuffer.length() ==0){
					stringBuffer.append(mapMenuUserList.get(i).getMenuId());
				}else{
					stringBuffer.append(",").append(mapMenuUserList.get(i).getMenuId());
				}
			}
		}
		return R.data(stringBuffer);
	}

	/**
	 * @描述：批量收藏菜单
	 * @作者: 卢顺
	 * @日期: 2022-12-07
	 */
	@PostMapping("/collectionBatch")
	@ApiOperation(value = "批量收藏菜单")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "menuIds", value = "菜单id列表（逗号分隔）", paramType = "query", dataType = "string")
	})
	@Transactional(rollbackFor = Exception.class)
	public R collectionBatch(String menuIds) {
		LambdaQueryWrapper<MapMenuUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(MapMenuUser::getUserId,getUser().getUserId().toString());
		menuUserService.remove(lambdaQueryWrapper);
		List<String> menuIdList = Arrays.asList(menuIds.split(","));
		List<MapMenuUser> mapMenuUsers = new ArrayList<>();
		for (int i =0;i<menuIdList.size();i++){
			MapMenuUser mapMenuUser = new MapMenuUser();
			mapMenuUser.setId(UuidUtil.get32UUID());
			mapMenuUser.setUserId(getUser().getUserId().toString());
			mapMenuUser.setMenuId(menuIdList.get(i));
			mapMenuUser.setCreator(getUser().getUserId().toString());
			mapMenuUser.setCreateTime(new Date());
			mapMenuUsers.add(mapMenuUser);
		}
		menuUserService.saveBatch(mapMenuUsers);
		return R.data("批量收藏成功");
	}

	private List<MapMenuUser> getInfo(){
		LambdaQueryWrapper<MapMenuUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(MapMenuUser::getUserId,getUser().getUserId().toString());
		List<MapMenuUser> mapMenuUserList = menuUserService.list(lambdaQueryWrapper);
		return mapMenuUserList;
	}
}
